<?php
include('http://deviet.ucdavis.edu/includes/head.cfm');
?>
<title>IET : Link Checker (Summary)</title>
<meta name="Description" content="Information and Educational Technology website">
<meta name="Keywords" content="IET, UCDavis IET">
</head>
<body> 
<div id="page_content">
<?php
include('http://deviet.ucdavis.edu/includes/navbar.cfm');
?>
<a name = 'top'></a>
<div id="main"> 
  <div id="main_wrapper"> 
    <div id="breadcrumb"> <a href="http://deviet.ucdavis.edu:80/">IET Home</a> : Link Checker (Summary)</div> 
    <!-- end breadcrumb --> 
    <h1>Link Checker (Summary)</h1>
		<div id="columnwrap">
		<div id="columna" class="hide">
		
		</div><!-- End columna -->
		<div id="columnb" class="content_right">
		
		
		<div class="section_nav">
			<h1>In This Section</h1>
			<ul>
				<li>
					<a href="#names" title="Same text, different URL">Same text, different URL</a></li><li>
					<a href="#urls" title="Same URL, different text">Same URL, different text</a></li><li>
					<a href="#missing" title="Missing pages">Missing pages</a></li>
			</ul>
		</div><!-- End section_nav -->
<?

    include ("htmlparser.inc");
    include ("link_utility.inc");
    
    //define("ROOT", 'http://deviet.ucdavis.edu');
    //define("BASE_URL", 'http://deviet.ucdavis.edu/');
    
    
    //define ("BASE_URL", $_POST['root']); TEMPORARILY DISABLED
    define ("BASE_URL", "http://deviet.ucdavis.edu");
    
    $other_servers = $_POST['other'] . ", " . BASE_URL;
    
    //$BASE_URL = "http://devftg.ucdavis.edu/"; //TODO determine later from root URL
    
    //list of all URLS; keys are link text
    $link_names = array();
    
    //list of all link text; keys are URLS
    $link_urls  = array();
    
    //list of all visited links
    $visited_links = array();
    
    //list of links to visit
    $to_visit = array();
    
    //maintain a list of broken links
    $broken = array();
    
    //list of link text that should be ignored for purposes of the summary
    $ignore = $_POST['ignore'];
    
    function checkLinkConsistency($parser, $main_url){
    	global $link_names;
    	global $link_urls;
    	global $visited_links;
    	global $to_visit;
    	global $broken;
    	global $other_servers;
    	global $ignore;
    	
    	  if ($parser->iNodeType == NODE_TYPE_ELEMENT and strtolower($parser->iNodeName) == "a") {      		
	                $url = $parser->iNodeAttributes["href"];	
	                
	                //if we haven't visited the link, add it to the 'to_visit' array
	                //first though, make sure that it's an absolute link; if not, make it one
	                //also make sure the link is in the root domain
	                if(!strstr($url, "http")){
	                	//don't allow duplicate slashes to happen
	                	if($url[0] == "/")
	                		$url = substr($url, 1, 9999);

	                	 //remove any '..' from the $url
	               		 $url = getAbsolutePath($main_url, $url) . str_replace('../', '', $url);
	               		 
	               		 //echo $url . " " . getAbsolutePath($main_url, $url) . " " . $main_url . "<br>";
	               		 //echo $url . "<br>";
	                }

	                //add the link to the 'to visit' list IF it's on the same domain (or in list of domains) and is not a PDF (or other file type TODO)
	                //if(!isset($visited_links[$url]) and strstr($url, BASE_URL) and !strstr($url, ".pdf"))
	                if(!isset($visited_links[$url]) and strstr($other_servers, getHost($url)) and !strstr($url, ".pdf"))
	                	$to_visit[$url] = 1;
	                	
	                //echo $other_servers . " and " . getHost($url) . "<br>";
	                	
	                //previous element was an anchor; parse the next token and grab the text
	                $parser->parse();
	                if($parser->iNodeType == NODE_TYPE_TEXT){
	                	
	                	/////////////////////////////////////////////////////////////////////////////
	                	//check to see if this text already exists
	                	//if the link exists and the URL is different, show error; else, set it
	                	
	                	//keep grabbing text until we hit the end of the link
	                	$text = '';
	                	
	                	
	                	
	                	while($parser->iNodeType != NODE_TYPE_ENDELEMENT){
	                		$text .= $parser->iNodeValue . ' ';
	                		$parser->parse();
	                	}
	                	
	                	//echo "<b>$text <a href = $main_url>$main_url</a></b><br>";
	                	//echo "__________________" . $text . "<br>";
	                	if(isset($link_names[$text])){
	                		
	                		//add this link url to the end of the list if we haven't seen it before
	                		//strip out all slashes because they were causing problems (e.g., http://google.com SHOULD equal http://google.com/
	                		if(!strstr(str_replace('/', '', $link_names[$text]), str_replace('/', '', $url)))
	                			$link_names[$text] .= " ||| <u>$text</u> (<a href = $url>$url</a>) CONTEXT: <a href = $main_url>$main_url</a>";
	                	}
	                	else{
	                		
	                		//make sure it doesn't point to itself
	                		if($url != $main_url)
	                			$link_names[$text] = "<u>$text</u> (<a href = $url>$url</a>) CONTEXT: <a href = $main_url>$main_url</a>";
	                	}
	                	
	                	////////////////////////////////////////////////////////////////////////////////
	                	//check to see if the link text is different from known URLs (And not itself)
	                	
	                		                		
	                		$text = str_replace(chr(10), "", $text);
	                		$text = str_replace(chr(11), "", $text);
	                		$text = str_replace(chr(12), "", $text);
	                		$text = str_replace(chr(13), "", $text);
	                		$text = str_replace(chr(9), "", $text);
	                	
	                	if(isset($link_urls[$url])){

	                		//add this link text to the end of the list if we haven't seen it before
	                		if(!strstr(str_replace(" ", "", $link_urls[$url]), str_replace(" ", "", $text))){
	                			
	                			//echo "Found ". str_replace(" ", "", $text) . " in " . str_replace(" ", "", $link_urls[$url]) . "<br>";
	                			
	                			$link_urls[$url] .= " ||| <u>" . $text  ." </u> (<a href = $url>$url</a>) CONTEXT: <a href = $main_url>$main_url</a>";
	                			//echo "*** $text ||| " . $link_urls[$url] . "<br>";
	                		}
	                	}
	                	else{
	                		$link_urls[$url] = "<u>$text</u> (<a href = $url>$url</a>) CONTEXT: <a href = $main_url>$main_url</a>";	
	                		//print_r($link_urls);	
	                	}	       			
	                } //end if text node

	        } //end if anchor
    }
    
    function linkSummary(){
 	
    	//refer to variables in the global scope
    	global $link_names;
    	global $link_urls;
    	global $visited_links;
    	global $to_visit;
    	global $broken;
    	global $other_servers;
    	global $ignore;
    echo "<h2>Using Root URL:<u> " . BASE_URL . "</u></h2>";
    echo "<i>Also visiting sites in the following domains: $other_servers</i>";
    echo "<br /><br />";
    echo "<a name = 'names'></a>";
    echo "<h2>The following links have the same link text, but different URLS</h2>";
    echo "<i>An example of this would be if you had two links on one of your sites, both called 'IET Services', with one pointing to a production server, and the other pointing to a development server.  From a user standpoint, 'IET Services' should go to the same place.</i>";
    echo "<br /><a href = '#top'>Back to the top</a>";
    $keys = array_keys($link_names);
    foreach($keys as $k){
    	//echo $link_names[$k] . "<br>";
    	
    	$matches = explode("|||", $link_names[$k]);
    	   	
    	//don't display email addresses -- they are incorrectly handled
    	if(count($matches) > 1 and validText($k, $ignore)){
    		echo "<table width = '1200'>";
    		echo "<tr><td><h3><u>$k</u></h3></td></tr>";
    		echo "<tr><td width = '600'><b>Link Text/ URL</b></td><td width = '600'><b>Context</b> (Link to the page where this link is found)</td></tr>";
    		echo "<ul>";
    		foreach($matches as $m){
    			$s = explode('CONTEXT', $m);
    			echo "<tr><td width = '600'><li>" . $s[0] . "</li></td><td width = '600'>" . $s[1] . "</td></tr>";
    		}
    		echo "</ul>";
    		echo "</table>";
    	}
    }
    
    echo "<br />";
    echo "<a name = 'urls'></a>";
    echo "<h2>The following links have the same URL, but different link text</h2>";
    echo "<i>An example of this is when you have two links on different pages of your site -- one with the text 'MyUCDavis' and the other 'Main WebPortal'.  If both of these links point to the same location, users might become confused when browsing the site.  Using the same naming conventions throughout a site provides a sense of consistency and order.</i>";
    echo "<br /><a href = '#top'>Back to the top</a>";
	$keys = array_keys($link_urls);
    foreach($keys as $k){
    	$matches = explode("|||", $link_urls[$k]);
    	
    	//don't display email addresses -- they are incorrectly handled
    	//if(count($matches) > 1 and !strstr($ignore, '@')){
    	if(count($matches) > 1 and validText($k, $ignore)){
    		echo "<table width = '1200'>";
    		echo "<tr><td><h3><u>$k</u></h3></td></tr>";
    		echo "<tr><td width = '600'><b>Link Text/ URL</b></td><td width = '600'><b>Context</b> (Link to the page where this link is found)</td></tr>";
    		echo "<ul>";
    		foreach($matches as $m){
    			$s = explode('CONTEXT', $m);
    			echo "<tr><td width = '600'><li>" . $s[0] . "</li></td><td width = '600'>" . $s[1] . "</td></tr>";
    		}
    		echo "</ul>";
    		echo "</table>";
    	}
    }
    
    echo "<br />";
    echo "<a name = 'missing'></a>";
    echo "<h2>The following links were (probably) not found</h2>";
    echo "<i>Check to see if these URLs make sense.  It is possible that the Spider has somehow constructed a bad URL, and that there shouldn't even be a page at the URL specified.</i><br />";
    echo "<a href = '#top'>Back to the top</a><br /><br />";
    //don't display any link with an @... it's probably an email address
    $keys = array_keys($broken);
    foreach($keys as $k){
    	if(validText($k, $ignore))
    		echo "<a href = $k>$k</a><br> ". strstr('@', $k);
    }
 }
    
    function checkHeaderConsistency($parser, $main_url){
    	global $link_names;
    	global $link_urls;
    	global $visited_links;
    	global $to_visit;
    	global $broken;
    	global $other_servers;
    	global $ignore;
    	
    	  if ($parser->iNodeType == NODE_TYPE_ELEMENT and strtolower($parser->iNodeName) == "a") {      		
	                $url = $parser->iNodeAttributes["href"];	              
	                //if we haven't visited the link, add it to the 'to_visit' array
	                //first though, make sure that it's an absolute link; if not, make it one
	                //also make sure the link is in the root domain
	                if(!strstr($url, "http")){
	                	//don't allow duplicate slashes to happen
	                	if($url[0] == "/")
	                		$url = substr($url, 1, 9999);

	                	 //remove any '..' from the $url
	               		 $url = getAbsolutePath($main_url, $url) . str_replace('../', '', $url);
	                }

	                //add the link to the 'to visit' list IF it's on the same domain (or in list of domains) and is not a PDF (or other file type TODO)
	                //if(!isset($visited_links[$url]) and strstr($url, BASE_URL) and !strstr($url, ".pdf"))
	                if(!isset($visited_links[$url]) and strstr($other_servers, getHost($url)) and !strstr($url, ".pdf"))
	                	$to_visit[$url] = 1;
	        } //end if anchor
	        
	        if ($parser->iNodeType == NODE_TYPE_ELEMENT and strtolower($parser->iNodeName) == "title") { 
	        	//previous element was an title; parse the next token and grab the text
	                $parser->parse();
	                
	                if($parser->iNodeType == NODE_TYPE_TEXT){
	                	$text = '';

	                	while($parser->iNodeType != NODE_TYPE_ENDELEMENT){
	                		$text .= $parser->iNodeValue . ' ';
	                		$parser->parse();
	                	}
	                	
	                	$length = strlen($text);
	                	$text = substr($text, 0, 50);
                		$text = "(" . $length . ") "  . $text; 
                		if($length > 50)
                			$text .= "...";                		
	                
	                	
	                	//flag titles longer than 64 characters
	                	if($length > 64)
	                		$text = "<b>" . $text;
	                	if($length > 64)
	                		$text .= "</b>";
	                	
	                }
	                
	                if(!isset($link_names[$main_url]["title"])){
		               	$link_names[$main_url]["title"] = $text;
		               	$link_names[$main_url]["url"] = $main_url;
}
	        }
	        
	        
	        
	        if ($parser->iNodeType == NODE_TYPE_ELEMENT and strtolower($parser->iNodeName) == "h1") { 
	        	//previous element was an title; parse the next token and grab the text
	                $parser->parse();
	                
	                if($parser->iNodeType == NODE_TYPE_TEXT){
	                	$text = '';

	                	while($parser->iNodeType != NODE_TYPE_ENDELEMENT){
	                		$text .= $parser->iNodeValue . ' ';
	                		$parser->parse();
	                	}
	                	
	                	$length = strlen($text);
	                	$text = substr($text, 0, 64);
                		$text = "(" . $length . ") "  . $text; 
                		if($length > 64)
                			$text .= "...";                		
	                	
	                }
	                
	                else {
	                	$text =  "Header contains no text";
	                }
	                	
	                
	                $header = $text;
	                
	                if(!isset($link_names[$main_url]["header"]))
	                	$link_names[$main_url]["header"] = $text;
	        }
       
	      
    }
    
    function headerSummary(){
    	global $link_names;
    	echo "<table width = '1400' cellpadding = 0 cellspacing = 0 border = 0>";
    	echo "<tr>";
    	foreach($link_names as $page){
    		//hide all anchors, don't want them
    		if(!strstr($page['url'], "#")){
	    		echo "<td width = '500'>";
	    		echo $page['title'];
	    		echo "</td>";
	    		echo "<td width = '500'>";
	    		echo $page['header'];
	    		echo "</td>";
	    		echo "<td width = '80'>";
	    		echo "</td>";
	    		echo "<td width = '320'>";
	    		echo "<a href = '" . $page['url'] . "'>" . $page['url'] . "</a>";
	    		echo"</td>";
	    		echo "</tr>";
    		}
    	}
    	echo "</table>";
    }

    function checkPage($main_url){
    		set_time_limit(0);
    		//echo "<B>$main_url</b>";
    	/**
    	 * Outputs a list of all 'errors' on a page
    	 * @param string $main_url The URL of the page to check
    	 * @return none
    	 */
    	
    	//echo $main_url . "<br>";
    	
    	//refer to variables in the global scope
    	global $link_names;
    	global $link_urls;
    	global $visited_links;
    	global $to_visit;
    	global $broken;
    	global $other_servers;
    	global $ignore;
    	
    	$parser = HtmlParser_ForURL ($main_url);
    	if($parser == null)
    		$broken[$main_url] = 1;
    	
    	while ($parser != null and $parser->parse()) {      
    		//if the element is an anchor tag...
    		
    		
    		/**
    		 * Put the function you want to run here
    		 */
    		checkHeaderConsistency($parser, $main_url);
    		//checkLinkConsistency($parser, $main_url);
	      
	    } //end while
	    
	    //page has been parsed... parse the next one
	    //if the current page is in 'to_visit', remove it
	    if(isset($to_visit[$main_url]))
	    	unset($to_visit[$main_url]);
	    	
	    //add to visited list
	    $visited_links[$main_url] = 1;
	    	
	    $keys = array_keys($to_visit);
	    foreach($keys as $site)
	    	if(!isset($visited_links[$site]))
	    		checkPage($site);
	    
    } //end function

 	checkPage(BASE_URL);

 	headerSummary();
 	
 	//linkSummary();
 	
 	
 
?>
		</div><!-- End columnb -->
</div><!-- End columnwrap -->
	</div><!-- end main_wrapper --> 
</div>
<!-- end main --> 

<?php
include('http://deviet.ucdavis.edu/includes/bottomnav.cfm');
?>
<!-- Footer Bar --> 
<div id="footer"> Contact: <a href="mailto:ietweb@ucdavis.edu">ietweb@ucdavis.edu</a>     Last modified: August 25, 2006     Copyright Regents of the University of California, 2006 </div> 
<!-- end footer --> 
<!-- End Footer Bar -->
</div>
</body>
</html>
